<?php

/**
 * 银行卡管理
 */

namespace app\member\common;

class CardCommon extends \app\member\common\MemberCommon {

    protected function index() {

        $realInfo = target('member/MemberReal')->getWhereInfo(['A.user_id' => $this->userInfo['user_id']]);
        if(empty($realInfo)) {
            $this->redirect(url('member/real/index'));
            exit;
        }
        $where = [];
        $where['A.user_id'] = $this->userInfo['user_id'];
        $info = target('member/PayCard')->getWhereInfo($where);

        if (!isPost()) {
            $this->setMeta('我的银行卡');
            $this->pageInfo['name'] = '我的银行卡';
            $this->assign('info', $info);
            $this->assign('realInfo', $realInfo);
            $this->assign('bankList', target('member/PayBank')->loadList());
            $this->memberDisplay();
        } else {
            $label = $_POST['label'];
            $bankInfo = target('member/PayBank')->getWhereInfo([
                'label' => $label
            ]);
            if(empty($bankInfo)) {
                $this->error('开户银行不存在！');
            }

            $type = request('post', 'valtype', 0, 'intval');
            if (!$type) {
                $receive = $this->userInfo['tel'];
            } else {
                $receive = $this->userInfo['email'];
            }

            if (empty($receive)) {
                $this->error('该验证方式未绑定，请使用其他验证方式！');
            }

            if (!target('member/Member', 'Service')->checkVerify($receive, $_POST['code'], 2)) {
                $this->error(target('member/Member', 'Service')->getError());
            }

            $_POST['name'] = $bankInfo['name'];
            $_POST['account_name'] = $realInfo['name'];
            $_POST['user_id'] = $this->userInfo['user_id'];
            $_POST['card_id'] = $info['card_id'];
            if(!target('member/PayCard')->saveData('edit')) {
                $this->error(target('member/PayCard')->getError());
            }
            $this->success('银行卡保存成功！');

        }
    }

}